//
//  RootViewController.m
//  LessonSandBox
//
//  Created by lanou on 15/9/14.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "RootViewController.h"
@interface RootViewController()

-(NSString *)docPath;
@property(nonatomic,strong)UILabel *label;
@end
@implementation RootViewController
-(NSString *)docPath
{
   return  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
  



}

-(void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(100, 40, 100, 40);
    
    [button addTarget:self action:@selector(sandBox) forControlEvents:(UIControlEventTouchUpInside)];
    [button setTitle:@"sandBox" forState:(UIControlStateNormal)];
    
    UIButton *writebutton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    writebutton.frame = CGRectMake(100, 100, 100, 40);
    
    [writebutton addTarget:self action:@selector(write) forControlEvents:(UIControlEventTouchUpInside)];
    [writebutton setTitle:@"write" forState:(UIControlStateNormal)];
    
    UIButton *readbutton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    readbutton.frame = CGRectMake(100, 150, 100, 40);
    
    [readbutton addTarget:self action:@selector(read) forControlEvents:(UIControlEventTouchUpInside)];
    [readbutton setTitle:@"read" forState:(UIControlStateNormal)];
   
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(10, 200, 300, 30)];
    self.label.text = @"显示文本";
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.font = [UIFont systemFontOfSize:20];
    
    
    
    [self.view  addSubview:button];
    [self.view addSubview:self.label];
    [self.view  addSubview:readbutton];
    [self.view addSubview:writebutton];
}

-(void)sandBox
{
    //获取沙盒路径 需要借助以下函数
    /**
     *  沙盒是一个文件夹 每一个应用程序安装完成的时候 系统就会自动生成一个沙盒文件夹  来存放当前应用程序所需要的持久性数据
     苹果对沙盒做了一个安全操作  当前应用程序不能访问当前沙盒之外的其他文件夹（沙盒）
     *IOS8.0之后的沙盒路径下  包含3个内容  ios8.0之前还包括.app文件 也就是我们所谓的程序包
     *
     1.Documents:存放文本信息的地方 
     2.Library -> Caches:存放缓存文件  数据量比较大得多媒体文件  如音频、视频、图片等
     Library->Preferences:NSUserDefaults 存储的用户偏好设置
     3.tmp：暂存区  半成品 比如未下载好的音频、视频  下载好后  系统会自动执行剪切操作  将其剪切到Caches文件夹
     */
    
    //获取沙盒路径
    NSString *homePath = NSHomeDirectory();
    NSLog(@"%@",homePath);
   // 有了沙盒路径  怎样去获取Documents路径  下面有三种方式
    //第一种%
    NSString *docPath = [homePath stringByAppendingString:@"/Documents"];
    NSLog(@"%@",docPath);
    //第二种
    NSString *docPath2 = [homePath stringByAppendingPathComponent:@"Documents"];
    NSLog(@"%@",docPath2);
    //第三种
NSString *docPath3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSLog(@"arr = %@",docPath3);
    
    
}


-(void)read
{
   //先获取到Documents路径
    NSString *docPath = [self docPath];
    //获取到a.txt文件的路径
    NSString *filePath = [docPath stringByAppendingPathComponent:@"a.txt"];
    
//    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//   
    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
    self.label.text = arr[0];
    
    
//    self.label.text = str;
    
    
    
    
}
-(void)write
{
    //获取Documents路径
    NSString *docPath = [self docPath];
   
    
    //创建一个.txt文件  在Documents路径下
    //如果a.txt文件不存在  此时会创建a.txt文件  如果写入的时候已经存在a.txt 会覆盖掉之前存在的a.txt
    NSString *filePath = [docPath stringByAppendingPathComponent:@"a.txt"];
 
    
    //将字符串写到本地的a.txt文件
    NSString *str = @"your mother biu biu biu";
    [str  writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];    
    
    
    
    
    
    
    NSArray *arr = [NSArray arrayWithObject:str];
    
    [arr  writeToFile:filePath atomically:YES];
    

}


@end
